/***********************************************
 * runcli.cpp 2011-08-15 22:56:00
 *            swhors@naver.com
 *
 ***********************************************/

#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "skycli.h"
#include "runcli.h"
#include "netcli.h"

int gRunState = 0;

void stopcli()
{
    gRunState = 1;
}

int runcli( char * aAddress, int aPort )
{
    fd_set         sReadFDS;
    struct timeval sTimeVal;

    int sRC          = SKY_RC_SUCCESS;
    int sState       = 0;
    int sSockFd      = 0;
    int sIgnoreInput = 0;
    int sReadCnt     = 0;
    int sCMP         = 0;
    int sRet         = 0;

    char sReadBuf[SKY_READBUF_LEN] = {0,};

    sSockFd = StartSock( aAddress, aPort );

    if( sSockFd > 0 )
    {
        gRunState = 1;
        printf( "Connected to %s:%d...\n", 
                aAddress,
                aPort );
        printf( "_____________________\n");
        while( gRunState )
        {
            FD_ZERO( &sReadFDS );
            FD_SET( SKY_SYSTEM_INFD, &sReadFDS );
            FD_SET( sSockFd, &sReadFDS );

            sTimeVal.tv_sec = 0;
            sTimeVal.tv_usec = 500;
            sState = select( SKY_SYSTEM_INFD + sSockFd + 1,
                             &sReadFDS,
                             NULL,
                             NULL,
                             &sTimeVal);
            if( sState > 0 )
            {
                if( FD_ISSET( SKY_SYSTEM_INFD, &sReadFDS ) )
                {
                    if( sIgnoreInput == 0 )
                    {
                        memset( sReadBuf,
                                0,
                                SKY_READBUF_LEN );
                        sReadCnt = read( 0,
                                         sReadBuf,
                                         SKY_READBUF_LEN );
                        if( sReadCnt > 0 )
                        {
                            sCMP = 0;

                            sReadBuf[sReadCnt-1] = 0;
                            sReadCnt--;

                            sCMP = memcmp( sReadBuf,
                                           SKY_STR_EXIT,
                                           sReadCnt );
                            if( sCMP == 0 )
                            {
                                CloseSock( sSockFd );
                                sRC = SKY_RC_SUCCESS;
                                break;
                            }
                            else
                            {
                                sRet = write( sSockFd,
                                              sReadBuf,
                                              sReadCnt );
                                if( sRet == sReadCnt )
                                {
                                    /* do nothing */
                                    /* in future, it will be
                                       controlled.. */
                                }
                                else
                                {
                                    /* do nothing */
                                    /* in future, it will be
                                       controlled.. */
                                }
                            }
                        }
                        else
                        {
                            /* do nothing */
                        }
                    }
                    else
                    {
                        sIgnoreInput = 0;
                    }
                }
                else
                {
                    /* do nothing */
                }

                if( FD_ISSET( sSockFd, &sReadFDS ) )
                {
                    memset( sReadBuf,
                            0,
                            SKY_READBUF_LEN );
                    sReadCnt = read( sSockFd,
                                     sReadBuf,
                                     SKY_READBUF_LEN );
                    if( sReadCnt > 0 )
                    {
                        sIgnoreInput = 1;
                        printf( "%s\n", sReadBuf );
                    }
                    else
                    {
                        /* do nothing */
                    }
                }
                else
                {
                    /* do nothing */
                }
            }
            else
            {
                if( sState == 0 )
                {
                    /* time out */
                    /* do nothing */
                }
                else
                {
                    /* error */
                    CloseSock( sSockFd );
                    break;
                }
            }
        }
    }
    else
    {
        /* do nothing */
        sRC = SKY_RC_FAIL;
        printf( "Fail to %s:%d...\n", 
                aAddress,
                aPort );
    }

    return sRC;
}
